package Serverlet;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.sf.json.JSONObject;
import tools.Lolita;

public class OrderServlet {
	public String order(String request) {
		
		Gson gson=new Gson();
        Map map = gson.fromJson(request, Map.class);
     //1：预定订单   
        int commo_id = (int) Double.parseDouble(map.get("commo_id").toString());
        int user_id = (int) Double.parseDouble(map.get("user_id").toString());
        String str = gson.toJson(map);
        
        JSONObject jsobj=gson.fromJson(str, JSONObject.class);
        try {
			new Lolita().AddLolita("order_",jsobj);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return "order success!";
	
	//2:显示最靠前
	}
	public String forward(String request) {
		 String driver ="org.mariadb.jdbc.Driver";
	        // url
	        String url = "jdbc:mariadb://vpn.lathyrus.love:3306/work";
	        // 用户名 密码
	        String user = "root", pass = "root";
	        Connection conn = null;
	        Statement stmt = null;
	        ResultSet rs = null;
	        Gson gson=new Gson();
	        
	        try {
	            // 注册数据库驱动
	            Class.forName(driver);
	            // 获取数据库连接
	            conn = DriverManager.getConnection(url,user,pass);
	            // 创建Statement对象
	            stmt = conn.createStatement();
	            // 要查询的语句
	            String sql = "select * from commodity where id in "+
	            "(select commo_id from (select commo_id from order_ group by commo_id limit 2) as id)";
	            // 执行查询
	            rs = stmt.executeQuery(sql);
	           
	         // 输出查询结果
	            ArrayList<Map> arr=new ArrayList();
	            while(rs.next()) {
	            Map<String ,String> map2 = new HashMap<String , String>();
	            map2.put("id",rs.getString(1));
	            map2.put("commoname",rs.getString(2));
	            map2.put("type",rs.getString(3));
	            map2.put("price",rs.getString(4));
	           arr.add(map2);
	            }
	            return gson.toJson(arr);

	            
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                // 关闭结果
	                if(rs!=null) {
	                    rs.close();
	                }
	                // 关闭载体
	                if(stmt!=null) {
	                    stmt.close();
	                }
	                // 关闭连接
	                if(conn!=null) {
	                    conn.close();
	                }
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
	        return "true";
		
		}
	public String pay(String request) throws SQLException {
		
		Gson gson=new Gson();
        Map map = gson.fromJson(request, Map.class);
        String str = gson.toJson(map.get("list"));
        System.out.println(str);
        int[] arr = gson.fromJson(str.substring(1,str.length()-1), int[].class);
        for(int in : arr) {
        	JSONObject a=new JSONObject();
        	a.put("commo_id",in);
        	a.put("type",1);
        	String str2 = gson.toJson(map.get("userid"));
        	a.put("user_id",Integer.valueOf(str2.substring(1,str2.length()-1)).intValue());
        	new Lolita().AddLolita("order_",a);
        }
		return "pay successful!";
	}
	
	public String history(String request){
		Gson gson=new Gson();
		Map map=gson.fromJson(request,Map.class);
		String str=gson.toJson(map.get("userid"));
		// 数据库驱动
        String driver ="org.mariadb.jdbc.Driver";
        // url
        String url = "jdbc:mariadb://vpn.lathyrus.love:3306/CBF";
        // 用户名 密码
        String user = "root", pass = "root";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 注册数据库驱动
            Class.forName(driver);
            // 获取数据库连接
            conn = DriverManager.getConnection(url,user,pass);
            // 创建Statement对象
            stmt = conn.createStatement();
            // 要查询的语句
            
            String sql = "";
            // 执行查询
            rs = stmt.executeQuery(sql);
           
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭结果
                if(rs!=null) {
                    rs.close();
                }
                // 关闭载体
                if(stmt!=null) {
                    stmt.close();
                }
                // 关闭连接
                if(conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return "true";
	}

}
